package com.szholly.data.general;

import com.szholly.utils.util.StringUtils;

/**
 * 类功能说明 定义ORM查询过滤接口 2014.6.4
 */
public class QueryFilter implements IQueryFilter {
	private String selectFields;
	private String groupByString;
	private String orderByString;
	private String whereString;
	private boolean addCaptionField;
	private GlobalParameter[] whereParameter;

	public QueryFilter() {
		selectFields = "*";
		whereString = "";
		groupByString = "";
		orderByString = "";
		addCaptionField = false;
	}

	/**
	 * 获取或设置是否添加字典的Caption列
	 */
	public void setAddCaptionField(boolean value) {
		addCaptionField = value;
	}

	/**
	 * 获取或设置是否添加字典的Caption列
	 */
	public boolean getAddCaptionField() {
		return this.addCaptionField;
	}

	/**
	 * 获取或设置查询字段列表
	 */
	public String getSelectFields() {
		if (selectFields == null || selectFields.trim().length() <= 0)
			selectFields = "*";
		return selectFields;
	}

	/**
	 * 获取或设置查询字段列表
	 */
	public void setSelectFields(String value) {
		selectFields = value;
	}

	/**
	 * 获取或设置查询条件
	 */
	public String getWhereString() {
		return whereString;
	}

	/**
	 * 获取或设置查询条件
	 */
	public void setWhereString(String value) {
		whereString = value;
	}

	/**
	 * 获取或设置查询条件参数
	 */
	public GlobalParameter[] getWhereParameter() {
		return whereParameter;
	}

	/**
	 * 获取或设置查询条件参数
	 */
	public void setWhereParameter(GlobalParameter[] value) {
		whereParameter = value;
	}

	/**
	 * 获取或设置GroupBy条件
	 */
	public String getGroupByString() {
		return groupByString;
	}

	/**
	 * 获取或设置GroupBy条件
	 */
	public void setGroupByString(String value) {
		groupByString = value;
	}

	/**
	 * 获取或设置序排字符串
	 */
	public String getOrderByString() {
		return orderByString;
	}

	/**
	 * 获取或设置序排字符串
	 */
	public void setOrderByString(String value) {
		orderByString = value;
	}

	@Override
	public String getKey() {
		String hashCode = "";
		if (!StringUtils.IsNullOrEmpty(this.getSelectFields()))
			hashCode += this.getSelectFields();
		else
			hashCode += "*";

		if (!StringUtils.IsNullOrEmpty(this.getWhereString()))
			hashCode += "_" + this.getWhereString();
		else
			hashCode += "_null";

		if (!StringUtils.IsNullOrEmpty(this.getGroupByString()))
			hashCode += "_" + this.getGroupByString();
		else
			hashCode += "_null";

		if (!StringUtils.IsNullOrEmpty(this.getOrderByString()))
			hashCode += "_" + this.getOrderByString();
		else
			hashCode += "_null";

		hashCode += "_" + this.getAddCaptionField();

		hashCode += "_" + getParaKey(this.whereParameter);
		return hashCode;
	}

	/**
	 * 获取参数的Key
	 */
	public static String getParaKey(GlobalParameter[] paras) {
		String hashCode = "";
		if (paras != null && paras.length > 0) {
			for (GlobalParameter para : paras) {
				hashCode += "_" + para.getParaName();
				if (para.getParaValue() != null) {
					hashCode += "_" + para.getParaValue().toString();
				} else {
					hashCode += "_null";
				}
			}
		}else{
			hashCode="_null";
		}
		return hashCode;
	}

	/**
	 * 转换查询SQL语句
	 */
	public String toSql(ITable table) {
		String sql = "";
		if (StringUtils.IsNullOrSpace(getSelectFields()))
			setSelectFields("*");

		if (getSelectFields().trim().equals("*"))
			sql = "select " + table.getSelectFields(false) + " from "
					+ table.getTableName();
		else
			sql = "select " + getSelectFields() + " from "
					+ table.getTableName();

		if (!StringUtils.IsNullOrSpace(getWhereString()))
			sql += " where " + getWhereString();

		if (!StringUtils.IsNullOrSpace(getGroupByString()))
			sql += " group by " + getGroupByString();

		if (!StringUtils.IsNullOrSpace(getOrderByString()))
			sql += " order by " + getOrderByString();
		return sql;
	}
}